<!DOCTYPE html>
<html>

<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>商品分类管理</title>
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <link rel="stylesheet" href="../plugins/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="../plugins/adminLTE/css/AdminLTE.css">
    <link rel="stylesheet" href="../plugins/adminLTE/css/skins/_all-skins.min.css">
    <link rel="stylesheet" href="../css/style.css">
	<script src="../plugins/jQuery/jquery-2.2.3.min.js"></script>
    <script src="../plugins/bootstrap/js/bootstrap.min.js"></script>
	<link rel="stylesheet" href="../plugins/select2/select2.css" />
	<link rel="stylesheet" href="../plugins/select2/select2-bootstrap.css" />
	<script src="../plugins/select2/select2.min.js" type="text/javascript"></script>
	<script>
		$(function () {
			getData();
			$("#addType").select2();
		})
		function getData(obj,pid) {
			$.ajax({
				url:"/item/getinfo",
				type: "post",
				data:{number:obj,pid:pid},
				dataType: "json",
				success:function (page) {
					var result = page.data.result;
					var b = page.data;
					var count = "";
					var parentId = result[0].parentId;
					$("#itemId").val(parentId);
					for (var i = 0; i < result.length; i++) {
						var id1 = result[i].id;
						var name1 = JSON.stringify(result[i].name);
						var parent1 = result[i].parentId;

						count+= '<tr >\n' +
								'\t\t\t                              <td><input  type="checkbox" value="'+result[i].id+'"></td>\t\t\t                              \n' +
								'\t\t\t\t                          <td>'+result[i].id+'</td>\n' +
								'\t\t\t\t\t\t\t\t\t      <td>'+result[i].name+'</td>\t\t\t\t\t\t\t\t\t    \n' +
								'\t\t\t\t\t\t\t\t\t      <td>'+result[i].typeId+'</td>\t\t\t\t\t\t\t\t\t      \n' +
								'\t\t                                  <td class="text-center">\t\t                                     \n' ;
						count += "<button class='btn bg-olive btn-xs' onclick='getTheLower("+id1+","+parent1+","+name1+")' >查询下级</button>";
						count +='\t\t                                 \t  <button type="button" onclick="toUpdate('+result[i].id+')" class="btn bg-olive btn-xs" data-toggle="modal" data-target="#updateModal" >修改</button>                                           \n' +
								'\t\t                                  </td>\n' +
								'\t\t\t                          </tr>'
					}
					$("#itemTable").html(count);

					//分页
					var pageinfo = "";
					pageinfo += '<li><a href="javascript:getData(1,'+parentId+')">首页</a></li>'
					if (b.pageNum > 1) {
						pageinfo += '<li class="threeword"><a href="javascript:getData(' + (b.pageNum - 1) +','+parentId+ ')">上一页</a></li>'
					}
					pageinfo += '<li class="threeword">第 ' + b.pageNum + ' 页</li>'
					if (b.pageNum < b.pages) {
						pageinfo += '<li class="threeword"><a href="javascript:getData(' + (b.pageNum + 1) +','+parentId+  ')">下一页</a></li>'
					}
					pageinfo += '<li class="threeword"><a href="javascript:getData(' + (b.pages) +','+parentId+  ')">末页</a></li>'
					$("#page").html(pageinfo);


				/*	var pids =document.getElementById("pidList");
					var li=document.createElement("li");
					li.innerHTML='<li><a href="javascript:getData(1,'+parentId+')" >珠宝</a></li>';
					pids.appendChild(li);*/
					// 获取ol中的所有a标签
					var li = $("#topTypeId").find("li").find("a");
// console.log(li);
					var text = "";
					$.each(li,function (i,e) {
						if (i == li.length - 1) {
							// 只有一条数据
							text += e.text;
						} else {
							text += e.text + " >> ";
						}
						// 获取最后一个a标签的id(作为父级id)
						var length = li.length;
						if (i == length - 1) {
							var id = e.id;
							// 创建一个span标签
							var l = document.createElement("span");
							// span标签中添加span
							l.innerHTML = "<span name='buttonAdd' value=" + id + ">" + text + "</span>";
							$("#addName").html(l);
						}
						var length1 = li.length;
						if (i == length1 - 1) {
							var id = e.id;
							// 创建一个span标签
							var l = document.createElement("span");
							// span标签中添加span
							l.innerHTML = "<span name='buttonAdd' value=" + id + ">" + text + "</span>";
							$("#updateName").html(l);
						}
					})



						},
				error:function () {
					alert("查询失败");
				}
			});
		}
		var parent_Id = '';
		function getTheLower(id1,parentId1,name1) {
			console.log(id1,parentId1,name1);
			parent_Id = id1;
			//添加顶部分类

			//创建一个li标签
			var li = document.createElement("li");
			//给li标签添加属性
			li.innerHTML="<a id='"+id1+"' onclick='addType("+id1+",this)'>"+name1+"</a>";
			//获取父标签
			var type = document.getElementById("topTypeId");
			type.appendChild(li);

			getData(1,parent_Id);
		}

		//点击顶部类型执行的方法 并删除元素
		function addType(id1,v) {
			//获取自身的父级标签的下面所以标签并删除
			$(v).parent("li").nextAll().remove();

			parent_Id = id1;
			getData(1,id1);
		}

		function toAdd() {
			var pid = $("#itemId").val();
			$.ajax({
				url:"/item/toAdd",
				type:"post",
				data:{pid:pid},
				dataType:"json",
				async:false,
				success:function (result) {
					alert(result.message);
					var slist = result.data;
					var scount = "";
					for (var i = 0;i<slist.length;i++){
						scount += '<option value="'+slist[i].id+'">'+slist[i].name+'</option>'
					}
					$("#addType").html(scount);
				},
				error:function (){
					alert("修改失败");
				}
			})
		}

		function addinfo() {
			var pid = $("#itemId").val();
			$.ajax({
				url:"/item/addinfo",
				type:"post",
				data:$("#addForm").serialize()+"&pid="+pid,
				dataType:"json",
				async:false,
				success:function (result) {
					alert(result.message);
					getSearch();
				},
				error:function (){
					alert("修改失败");
				}
			})
		}

		function deleteAll(){
			var ids = [];
			var rows = $("input:checked");
			$(rows).each(function (i,e) {
				ids.push(e.value);
			})
			if (ids.length<=0){
				alert("请选择需要删除的内容");
				return ;
			}
			$.ajax({
				url:"/item/deleteAll",
				type:"post",
				data:{ids:ids},
				dataType:"json",
				success:function (result) {
					alert(result.message);
					getSearch();
				},
				error:function () {
					alert("删除失败");
				}
			})
		}
		function toUpdate(obj) {
			$.ajax({
				url:"/item/toUpdate",
				type:"post",
				data:{id:obj},
				dataType:"json",
				async:false,
				success:function (result) {
					var item = result.data.item;
					var slist = result.data.list;
					$("#id").val(item.id);
					$("#name").val(item.name);
					var scount = "";
					for (var i = 0;i<slist.length;i++){
						if (item.typeId == slist[i].id){
							scount += '<option selected value="'+slist[i].id+'">'+slist[i].name+'</option>'
						}
						scount += '<option value="'+slist[i].id+'">'+slist[i].name+'</option>'
					}
					$("#updateType").html(scount);
				},
				error:function (){
					msg("修改失败");
				}
			})
		}

		function updateinfo() {
			var pid = $("#itemId").val();
			$.ajax({
				url:"/item/updateinfo",
				type:"post",
				data:$("#updateForm").serialize()+"&pid="+pid,
				dataType:"json",
				async:false,
				success:function (result) {
					alert(result.message);
					getSearch();
				},
				error:function (){
					alert("修改失败");
				}
			})
		}


		function reload(){
			window.location.reload();
		}

		function getSearch() {
			location.href = "/item/toItem";
		}
	</script>
    
</head>

<body class="hold-transition skin-red sidebar-mini" >
  <!-- .box-body -->
                
                    <div class="box-header with-border">
                        <h3 class="box-title">商品分类管理     
                       	</h3>
                    </div>

                    <div class="box-body">
                  			<!-- <ol class="breadcrumb" id="pidList">
                        		<li>
		                        	<a href="javascript:getData(1,0)" >顶级分类列表</a>
		                        </li>
		                        &lt;!&ndash;<li>
		                       		<a href="#" >珠宝</a>
		                        </li>
		                        <li>
		                        	<a href="#" >银饰</a>
		                        </li>&ndash;&gt;&ndash;&gt;
	                        </ol>-->
						<ol class="breadcrumb" id="topTypeId">
							<li>
								<a onclick="reload()" >顶级分类列表</a>
							</li>
						</ol>

                        <!-- 数据表格 -->
                        <div class="table-box">
							
                            <!--工具栏-->
                            <div class="pull-left">
                                <div class="form-group form-inline">
                                    <div class="btn-group">
                                        <button type="button" class="btn btn-default" title="新建" data-toggle="modal" data-target="#editModal" onclick="toAdd()"><i class="fa fa-file-o"></i> 新建</button>
                                        <button type="button" class="btn btn-default" title="删除" onclick="deleteAll()"><i class="fa fa-trash-o"></i> 删除</button>
                                        <button type="button" class="btn btn-default" title="刷新" ><i class="fa fa-check"></i> 刷新</button>
                                       
                                    </div>
                                </div>
                            </div>                          
                       		
                        
			                <!--数据列表-->
			                  <table id="dataList" class="table table-bordered table-striped table-hover dataTable">
			                      <thead>
			                          <tr>
			                              <th class="" style="padding-right:0px">
			                                  <input type="checkbox" class="icheckbox_square-blue">
			                              </th> 
										  <th class="sorting_asc">分类ID</th>
									      <th class="sorting">分类名称</th>									   
									      <th class="sorting">类型模板ID</th>
									     						
					                      <th class="text-center">操作</th>
			                          </tr>
			                      </thead>
			                      <tbody id="itemTable">
			                          <!--<tr >
			                              <td><input  type="checkbox" ></td>			                              
				                          <td>982</td>
									      <td>吊坠/项链</td>									    
									      <td>
									      	11    
									      </td>									      
		                                  <td class="text-center">		                                     
		                                      <button type="button" class="btn bg-olive btn-xs" >查询下级</button> 		                                     
		                                 	  <button type="button" class="btn bg-olive btn-xs" data-toggle="modal" data-target="#editModal" >修改</button>                                           
		                                  </td>
			                          </tr>
			                      </tbody>-->
			                  </table>
							<input type="hidden" id="itemId">
			                  <!--数据列表/-->
							<div class="pageNum" id="page">
							</div>
                        </div>
                        <!-- 数据表格 /-->
                        
                        
                        
                        
                     </div>
                    <!-- /.box-body -->
              
                                
<!-- 增加窗口 -->
  <form id="addForm">
	  <div class="modal fade" id="editModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
		  <div class="modal-dialog" >
			  <div class="modal-content">
				  <div class="modal-header">
					  <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
					  <h3 id="myModalLabel">商品分类编辑</h3>
				  </div>
				  <div class="modal-body">

					  <table class="table table-bordered table-striped"  width="800px">
						  <tr>
							  <td>上级商品分类</td>
							  <td id="addName">
								  <!--珠宝 >>  银饰-->
							  </td>
						  </tr>
						  <tr>
							  <td>商品分类名称</td>
							  <td><input   class="form-control" placeholder="商品分类名称" name="name">  </td>
						  </tr>
						  <tr>
							  <td>类型模板</td>
							  <td>
								  <select id="addType" style="width:100%"  name="typeId">

								  </select>
							  </td>
						  </tr>
					  </table>

				  </div>
				  <div class="modal-footer">
					  <button class="btn btn-success" data-dismiss="modal" aria-hidden="true" onclick="addinfo()">保存</button>
					  <button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
				  </div>
			  </div>
		  </div>
	  </div>
  </form>


  <!-- 修改窗口 -->
  <form id="updateForm">
	  <div class="modal fade" id="updateModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
		  <div class="modal-dialog" >
			  <div class="modal-content">
				  <div class="modal-header">
					  <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
					  <h3 id="updateModalLabel">商品分类编辑</h3>
				  </div>
				  <div class="modal-body">

					  <table class="table table-bordered table-striped"  width="800px">
						  <tr>
							  <td>上级商品分类</td>
							  <td id="updateName">
								  <!--珠宝 >>  银饰-->
							  </td>
						  </tr>
						  <tr>
							  <td>商品分类名称</td>
							  <td><input id="name" name="name"  class="form-control" placeholder="商品分类名称">
								  <input type="hidden" name="id" id="id">
							  </td>
						  </tr>
						  <tr>
							  <td>类型模板</td>
							  <td>
								  <select id="updateType" style="width:100%"  name="typeId">

								  </select>
							  </td>
						  </tr>
					  </table>

				  </div>
				  <div class="modal-footer">
					  <button class="btn btn-success" data-dismiss="modal" aria-hidden="true" onclick="updateinfo()">保存</button>
					  <button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
				  </div>
			  </div>
		  </div>
	  </div>
  </form>

</body>
</html>